<template>
  <!-- 主体区域 -->
  <section id="app">
    <!-- 输入框 -->
    <TodoHeader @add="onadd" />
    <!-- 列表区域 -->
    <TodoMain :list="list" @del="ondel" />
    <!-- 统计和清空 -->
    <TodoFooter @clear="onclear" :length="list.length" />
  </section>
</template>

<script>
import TodoFooter from './components/TodoFooter.vue';
import TodoHeader from './components/TodoHeader.vue';
import TodoMain from './components/TodoMain.vue';

export default {
  components: {
    TodoMain,
    TodoFooter,
    TodoHeader
  },
  data() {
    return {
      list: JSON.parse(localStorage.getItem('TodoList')) || [
        { id: 1, name: '吃饭' },
        { id: 2, name: '睡觉' },
        { id: 3, name: '打游戏' }]
    }
  },
  methods: {
    onadd(value) {
      this.list.unshift({
        id: Date.now(),
        name: value
      })
    },
    ondel(id) {
      this.list = this.list.filter(item => item.id !== id)
    },
    onclear() {
      this.list = []
    }
  },
  watch: {
    list: {
      handler() {
        localStorage.setItem('TodoList', JSON.stringify(this.list))
      },
      deep: true
    }
  }
}
</script>

<style></style>
